package top.tenebrous.yzl.utils;

import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.codec.binary.Base64;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author hrz
 */
public class SignTest {
    private final static String PUBLIC_KEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCM5o8GZ9rzeWmFbE4dHVNOC34gTo/eBiqATlHv\n" +
            "u2wB25Jpj8qxXkUIQeCK7rvaQU09nfia4lWUcVlkbCd9P8X5mjHaE/Bzzmwhs7PpGGhh+p2Mr0dI\n" +
            "EDmIqlpAvfxelAEDue4irTpDF8tJBR6oXGT5ctNrfxjOXSxXwSGbfWMUawIDAQAB";


    public static void main(String[] args) throws Exception {
        Map<String, Object> params = new HashMap<>();
        params.put("aa", "ceshi");
        params.put("bb", "ceshi");
        params.put("dd", "ceshi");
        params.put("d1d", "ceshi");
        params.put("bb1", "ceshi");
        params.put("a1", "ceshi");

        LinkedHashMap<String, Object> sortedParam = MapUrlParamsUtils.sortByKey(params);
        String verifyStr = MapUrlParamsUtils.getUrlParamsByMap(sortedParam);
        RSA rsa = new RSA(null, PUBLIC_KEY);
        byte[] encryptByte = rsa.encrypt(verifyStr.getBytes(), KeyType.PublicKey);
        String encryptStr = Base64.encodeBase64String(encryptByte);
        System.out.println("encryptStr:" + encryptStr);
        //最终sign放入参数到里
        params.put("sign", encryptByte);


        //---公钥解密-----------------
        byte[] decodeByte = Base64.decodeBase64(encryptStr);
        byte[] decryptByte = rsa.decrypt(decodeByte, KeyType.PublicKey);
        String decryptStr = new String(decryptByte);
        System.out.println("decryptStr:" + decryptStr);
        System.out.println("---------------------------");
    }

//    @Test
//    public void orderPush() {
//        CanteenPacketPushParam orderPushParam = new CanteenPacketPushParam();
//        String id = SnowflakeIdUtils.getId();
//        orderPushParam.setBizOrderNo(id);
//        orderPushParam.setUserId(1093065L);
//        orderPushParam.setAmount(101L);
//        ResponseResult responseResult = requestJson("http://localhost:8080/fingertip/canteen/packet/push", orderPushParam);
//        System.out.println(JSONObject.toJSONString(responseResult));
//    }
//
//    @Test
//    public void queryOpenId() {
//        CanteenOpenIdParam canteenOpenIdParam = new CanteenOpenIdParam();
//        canteenOpenIdParam.setUserId(1093065L);
//
//        ResponseResult responseResult = requestJson("http://localhost:8080/fingertip/canteen/openId/wxa", canteenOpenIdParam);
//        System.out.println(JSONObject.toJSONString(responseResult));
//    }
//
//    @Test
//    public void openIdVerify() {
//        CanteenOpenIdVerifyParam verifyParam = new CanteenOpenIdVerifyParam();
//        verifyParam.setReqid("");
//        verifyParam.setAppkey("");
//        verifyParam.setTimestamp("");
//        verifyParam.setOpenid("");
//        verifyParam.setCode("");
//        verifyParam.setSign("");
//        verifyParam.setUserId(1093065L);
//        ResponseResult responseResult = requestJson("http://localhost:8080/fingertip/canteen/order/push", verifyParam);
//        System.out.println(JSONObject.toJSONString(responseResult));
//    }
//
//
    public static Map requestJson(String url, Object obj) {
        Map<String, Object> map = JSONObject.parseObject(JSONObject.toJSONString(obj), Map.class);
        LinkedHashMap<String, Object> sortedParam = MapUrlParamsUtils.sortByKey(map);
        String verifyStr = MapUrlParamsUtils.getUrlParamsByMap(sortedParam);
        RSA rsa = new RSA("", PUBLIC_KEY);
        byte[] encryptByte = rsa.encrypt(verifyStr.getBytes(), KeyType.PublicKey);
        String encryptStr = Base64.encodeBase64String(encryptByte);

        map.put("cgSign", encryptStr);

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.valueOf("application/json;UTF-8"));
        RestTemplate restTemplate = new RestTemplate();
        //封装请求头
        HttpEntity<Object> formEntity = new HttpEntity<>(map, headers);
        //3. 有请求头，没参数，result3.getBody()获取响应参数
        ResponseEntity<Map> responseEntity = restTemplate.postForEntity(url, formEntity, Map.class);
        return responseEntity.getBody();

    }

}
